var comPageNum = 1;//分页公共页码
/*用于编辑轮播图时的判定*/
var pictureId = null;

/*页面加载函数*/
$(function () {
    seahsdfs(1);
    pictureId = null;
});

/*跳转到添加页面*/
function jumpToAdd(title, url) {
    pictureId = null;
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

/*跳转到编辑页面*/
function jumpToEdit(title, url, id) {
    pictureId = id;
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

// 搜索条件校验规则
$.validator.addMethod("titleCheck", function (value, element) {
    var tel = /^[A-Za-z0-9\u4e00-\u9fa5]{1,60}$/;
    return this.optional(element) || (tel.test(value));
}, "输入有误");


// 保存信息更新或修改
$("#form-member").validate({
    rules: {
        title: "titleCheck"
    },
    submitHandler: function (form) {
        seahsdfs(1);
    }
})

/*表格渲染*/
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];

function seahsdfs(pageNum) {
    comPageNum = pageNum;
    $.ajax({
        url: root.sroreinter.query_slide_pictureBypage,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        async: false,
        data: {
            "pageNum": pageNum,
            "pageSize": 10,
            "title": $("#title").val(),
            "start_time":$("#start_time").val(),
            "end_time": $("#end_time").val(),
            "state": $("#state").find("option:selected").val()
        },
        success: function (result) {
            if(result.list.length==0&&comPageNum!=1){
                seahsdfs(comPageNum-1);
                return;
            }
            detalist(result.list);
            $('.M-box3').pagination({
                pageCount: result.totalPage,
                current:comPageNum,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    comPageNum = api.getCurrent();
                    seahsdfs(api.getCurrent());
                }
            });
        },
        error: function () {
            $.Huimodalalert('失败！', 2000);
        }
    });
}
function detalist(data) {
    dataSource.splice(0, dataSource.length);//清空数组
    $.each(data, function (i, field) {
        var rowData = new Array();
        rowData.push(field.rowNo);
        rowData.push(field.title);
        rowData.push(field.start_time);
        rowData.push(field.end_time);
        rowData.push(field.state);
        rowData.push(field.seq);
        rowData.push(field.creator_name);
        rowData.push(field.created_at);
        rowData.push(field.id);
        dataSource.push(rowData);
    });
    initDataTable(dataSource);
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,  // 自动宽度
            "ordering": false,   // 开启排序
            "searching": false,  //
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='../../../static/h-ui.admin/images/loading.gif' />",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        if (row[4] == 10) {
                            var val = '<span style="display: inline-block;width: 210px;">未生效</span>';
                        } else if (row[4] == 20) {
                            var val = '<span style="display: inline-block;width: 210px;">生效</span>';
                        } else if (row[4] == 30) {
                            var val = '<span style="display: inline-block;width: 210px;">失效</span>';
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span><a class="powerButton 233 " onclick="jumpToEdit(\'轮播图-编辑\',\'picture_detail.html\',\'' + row[8] + '\')">编辑</a>&nbsp;&nbsp;&nbsp;<a class="powerButton 234 " onclick="deleteSlidePicture(\'' + row[8] + '\')">删除</a></span>';
                        return val;
                    },
                    "targets": 8
                }
            ]
        });
        //权限控制函数
        power()
    };
}


/*删除轮播图*/
function deleteSlidePicture(id) {
    layer.confirm('你确定要删除吗？', function (index) {
        var quzhi = JSON.parse(web.readCookie("loginData"))
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            type: "post",
            url: root.sroreinter.delete_slide_picture,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                "id": id,
                "userId": quzhi.user.user_ID,
                "userName": quzhi.user.username
            },
            //传入组装的参数
            success: function (data, user_data, succCallBack) {
                if (data.errorCode == 0) {
                    layer.msg(data.errorMessage, {icon: 6, time: 1000});
                    seahsdfs(comPageNum);
                } else {
                    layer.msg(data.errorMessage, {icon: 5, time: 1000});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest);
            }
        })
    });
}

